<!-- eslint-disable prettier/prettier -->
<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="pr-1 m-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button
          type="primary"
          @click="handleTaskStatus({ eventType: 'tote_load' })"
          v-auth="'wms:task:wmsEssTask:edit'"
        >
          <Icon icon="ant-design:export-outlined" /> {{ t('取箱') }}
        </a-button>
        <a-button type="primary" @click="handleTaskStatus({ eventType: 'tote_unload'})" v-auth="'wms:task:wmsEssTask:edit'">
          <Icon icon="ant-design:import-outlined" /> {{ t('放箱') }}
        </a-button>
        <a-button type="primary" @click="handleTaskStatus({ eventType: 'task', status: 'success' })" v-auth="'wms:task:wmsEssTask:edit'">
          <Icon icon="ant-design:check-outlined" /> {{ t('完成') }}
        </a-button>
        <a-button type="primary" @click="handleTaskStatus({ eventType: 'task', status: 'cancel' })" v-auth="'wms:task:wmsEssTask:cancel'">
          <Icon icon="ant-design:close-outlined" /> {{ t('取消') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsWmsTaskWmsEssTaskList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    wmsEssTaskDelete,
    wmsEssTaskListData,
    wmsEssTaskCallback,
  } from '/@/api/wms/task/wmsEssTask';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { treeData } from '/@/api/bas/wms/area/bas3dwhArea';
  import InputForm from './form.vue';

  const { t } = useI18n('wms.task.wmsEssTask');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('任务指令管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('任务号'),
        field: 'code',
        component: 'Input',
        componentProps: {
          allowClear: true,
        },
      },
      {
        label: t('任务类型'),
        field: 'taskType',
        component: 'Select',
        componentProps: {
          dictType: 'wms_ess_task_type',
          allowClear: true,
        },
      },
      {
        label: t('业务类型'),
        field: 'busType',
        component: 'Select',
        componentProps: {
          dictType: 'wms_ess_task_bus_type',
          allowClear: true,
        },
      },
      {
        label: t('业务key'),
        field: 'busKey',
        component: 'Input',
      },
      {
        label: t('箱子编码'),
        field: 'boxCode',
        component: 'Input',
      },
      {
        label: t('缓存货位编码'),
        field: 'posCode',
        component: 'Input',
      },
      {
        label: t('所属区域'),
        field: 'areaCode',
        component: 'TreeSelect',
        componentProps: {
          api: treeData,
          // params: { isLoadUser: true, userIdPrefix: '' },
          // canSelectParent: false,
          allowClear: true,
        },
      },
      {
        label: t('指令'),
        field: 'command',
        component: 'Input',
      },
      {
        label: t('完成日期'),
        field: 'overDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('错误编码'),
        field: 'errCode',
        component: 'Input',
      },
      {
        label: t('错误描述'),
        field: 'errMsg',
        component: 'Input',
      },
      {
        label: t('任务状态'),
        field: 'taskStatus',
        component: 'Select',
        componentProps: {
          dictType: 'wms_ess_task_status',
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('任务号'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 200,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('任务类型'),
      dataIndex: 'taskType',
      key: 'a.task_type',
      sorter: true,
      width: 150,
      align: 'center',
      dictType: 'wms_ess_task_type',
    },
    {
      title: t('业务类型'),
      dataIndex: 'busType',
      key: 'a.bus_type',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'wms_ess_task_bus_type',
    },
    {
      title: t('业务key'),
      dataIndex: 'busKey',
      key: 'a.bus_key',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('箱子编码'),
      dataIndex: 'boxCode',
      key: 'a.box_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('缓存货位编码'),
      dataIndex: 'posCode',
      key: 'a.pos_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('所属区域编码'),
      dataIndex: 'areaCode',
      key: 'a.area_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('指令'),
      dataIndex: 'command',
      key: 'a.command',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('完成日期'),
      dataIndex: 'overDate',
      key: 'a.over_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('错误编码'),
      dataIndex: 'errCode',
      key: 'a.err_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('错误描述'),
      dataIndex: 'errMsg',
      key: 'a.err_msg',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('任务状态'),
      dataIndex: 'taskStatus',
      key: 'a.task_status',
      sorter: true,
      width: 130,
      align: 'center',
      fixed: 'right',
      dictType: 'wms_ess_task_status',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑任务指令'),
        onClick: handleForm.bind(this, { code: record.code }),
        auth: 'wms:task:wmsEssTask:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除任务指令'),
        popConfirm: {
          title: t('是否确认删除任务指令'),
          confirm: handleDelete.bind(this, { code: record.code }),
        },
        auth: 'wms:task:wmsEssTask:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getSelectRows }] = useTable({
    api: wmsEssTaskListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox', //'radio',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await wmsEssTaskDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  async function handleTaskStatus(data) {
    let arr = await getSelectRows();
    if (arr.length == 0) {
      showMessage(t('请先选择一行数据'));
      return;
    }
    const promises = arr.map(function (item) {
      const params = {
        taskCode: item.code,
        ...data,
      };
      return wmsEssTaskCallback({}, params);
    });
    Promise.all(promises)
      .then(function (posts) {
        console.log('posts', posts);
        showMessage('修改成功！');
        handleSuccess();
      })
      .catch(function (reason) {
        console.log('出错了', reason);
      });
  }
</script>
